package com.seasugar.raggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seasugar.raggie.model.correlation.CategorySetmeal;
import com.seasugar.raggie.model.dto.SetmealDto;
import com.seasugar.raggie.model.entity.Category;
import com.seasugar.raggie.model.entity.Setmeal;
import com.seasugar.raggie.mapper.SetmealMapper;
import com.seasugar.raggie.model.correlation.SetmealDish;
import com.seasugar.raggie.model.vo.SetmealVo;
import com.seasugar.raggie.service.CategorySetmealService;
import com.seasugar.raggie.service.SetmealDishService;
import com.seasugar.raggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategorySetmealService categorySetmealService;

    @Transactional
    @Override
    public boolean insertSetmeal(SetmealDto setmealDto) {
        this.save(setmealDto);
        Long setmealDtoId = setmealDto.getId();
        //添加关联表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDtoId);
            setmealDishService.save(setmealDish);
        }
        //添加外键
        categorySetmealService.save(new CategorySetmeal(setmealDto.getCategoryId(),setmealDtoId));
        return true;
    }

    /**
     * 修改时获取回显信息
     * @param id
     * @return
     */
    @Override
    public SetmealVo getSetmealById(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealVo setmealVo=new SetmealVo();
        BeanUtils.copyProperties(setmeal,setmealVo);
        List<SetmealDish> list=new LinkedList<>();
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);
        for (SetmealDish setmealDish : setmealDishList) {
            list.add(setmealDish);
        }
        setmealVo.setSetmealDishes(list);
        return setmealVo;
    }

    /**
     * 根据ids删除
     * @param list
     * @return
     */
    @Transactional
    @Override
    public boolean deleteByIds(List<String> list) {
        //先删除套餐
        this.removeByIds(list);
        //再删除套餐菜品关联表
        for (String s : list) {
            LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId,s);
            setmealDishService.remove(queryWrapper);
        }
        //再删除套餐分类关联表
        for (String s : list) {
            LambdaQueryWrapper<CategorySetmeal> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(CategorySetmeal::getSetmealId,s);
            categorySetmealService.remove(queryWrapper);
        }
        return true;
    }

    /**
     * 修改
     * @param setmealDto
     * @return
     */
    @Transactional
    @Override
    public boolean updateSetmealById(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        //修改setmeal_dish表

        //最简单的做法：先把原id所属菜品全部删除，然后重新添加
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //添加
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishService.save(setmealDish);
        }

        //修改外键表
        LambdaQueryWrapper<CategorySetmeal> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(CategorySetmeal::getSetmealId,setmealDto.getId());
        CategorySetmeal categorySetmeal = categorySetmealService.getOne(queryWrapper1);
        categorySetmeal.setCategoryId(setmealDto.getCategoryId());
        categorySetmealService.updateById(categorySetmeal);

        return true;
    }
}
